
exports.install = function (Vue, options) {


	Vue.prototype.random_num = function(minNum,maxNum){
		switch(arguments.length){ 
	        case 1: 
	            return parseInt(Math.random()*minNum+1,10); 
	        break; 
	        case 2: 
	            return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
	        break; 
            default: 
                return 0; 
            	break; 
	    } 
    };

    Vue.prototype.sheets_select = function(sheet_id){
    	this.$router.push(`/sheets/details/${sheet_id}`);
    }

    Vue.prototype.sheets_play = function(sheet_id){
  		var _loading = this.Loading;
  		var loading = _loading();

		let _index;

    	this.$api.get(this.ApiPath.sheets.getSheetDetail,{
			id: sheet_id
		},success=>{
			this.$store.commit('_set_play_list', { tracks: success.data.playlist.tracks, id: success.data.playlist.id ,privileges: success.data.privileges });
			switch(this.$store.getters.play_mode){
				case 0:
					_index = this.random_num(0,success.data.privileges.length);
					break;
				case 1:
				case 2:
				default:
					_index = 0;
					break;
			}
			
			loading ? loading.close() : '';

			this.$nextTick(()=>{
				if(success.data.privileges[_index].st == '-100'){
					// this.$alert(this._GLOBAL.msg.ST_100_ERROR);
					// return;
				}
				this.check_music(success.data.playlist['tracks'][_index])

				// console.log(this._GLOBAL.config.play_mode[this.$store.getters.play_mode])
				// console.log(this._GLOBAL.config.play_mode_text[this._GLOBAL.config.play_mode[this.$store.getters.play_mode]])
			})

		},fail=>{
			this.$alert(this._GLOBAL.msg.MUSIC_ERROR);
			loading ? loading.close() : '';
		})
    }

    Vue.prototype.check_music = function(item){
    	var _loading = this.Loading;
  		var loading = _loading();
  		
		this.$api.get(this.ApiPath.check.music,{
			id: item.id
		},success=>{
			var song = '';
			loading ? loading.close() : '';
			item['ar'].map((s,k)=>{
				song+=(s.name+ (k>1 ? '/': ''));
			});
			this.$store.commit('_set_current_music_play' , { id : item.id, name: item.name , desc: song + '-' + item.al.name , thumb: item.al.picUrl})
		},fail=>{
			loading ? loading.close() : '';
			this.$alert(fail.data.message);
		})
	}

};
